#ifndef NODE_H
#define NODE_H

#include "Surface.h"
#include "Box.h"
#include "Face.h"
#include "Vertex.h"
#include <algorithm>


typedef enum { X, Y, Z } Axis;
bool compareByX(Face *f1, Face *f2);
bool compareByY(Face *f1, Face *f2);
bool compareByZ(Face *f1, Face *f2);
vector<Face*> sortByAxis(vector<Face*> faces, Axis axis);

class Node: public Surface
{
public:
    Node(vector<Face*> faces, int level, int facesPerNode);
    ~Node();

    virtual bool hit(const Ray& r, float tmin, float tmax, SurfaceHitRecord& closestHit) const;
    virtual bool shadowHit(const Ray& r, float tmin, float tmax) const;

    void Render(int left, int right, int level);

    void Render(int depth);
    Axis bestAxis(vector<Face *> &faces);
private:
    Node *left, *right;
    Box box;
    vector<Face*> faces;
    int level;
    Axis axisSplited;

    int facesPerNode;


   inline bool isLeaf() const;
};


#endif // NODE_H
